//------------------------------------------------------------------------------
// Copyright (c) 2012 by Silicon Laboratories.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Silicon Laboratories End User
// License Agreement which accompanies this distribution, and is available at
// http://developer.silabs.com/legal/version/v10/License_Agreement_v10.htm
// Original content and implementation provided by Silicon Laboratories.
//------------------------------------------------------------------------------

//==============================================================================
// WARNING:
//
// This file is auto-generated by AppBuilder and should not be modified.
// Any hand modifications will be lost if the project is regenerated.
//==============================================================================
// library
// hal
#include <si32_device.h>
#include <SI32_CLKCTRL_A_Type.h>
#include <SI32_RTC_B_Type.h>
// application
#include "gRTC0.h"

#define   ONE_SECOND    0x8000    // When using a 32.768 clock, one second is
                                  // 32768 counts (0x8000).

//==============================================================================
//CONFIGURATION FUNCTIONS
//==============================================================================
void gRTC0_enter_off_config(void)
{
   // DISAABLE INTERRUPTS
   SI32_RTC_B_disable_alarm0(SI32_RTC_0);
   SI32_RTC_B_disable_alarm1(SI32_RTC_0);
   SI32_RTC_B_disable_alarm2(SI32_RTC_0);
   NVIC_ClearPendingIRQ(RTC0ALRM_IRQn);
   NVIC_ClearPendingIRQ(RTC0FAIL_IRQn);
   NVIC_DisableIRQ(RTC0ALRM_IRQn);
   NVIC_DisableIRQ(RTC0FAIL_IRQn);

   // DISABLE MODULE
   SI32_RTC_B_disable_rtc_timer(SI32_RTC_0);

   // DISABLE CLOCK
   SI32_CLKCTRL_A_disable_apb_to_modules_0(SI32_CLKCTRL_0,
                                           SI32_CLKCTRL_A_APBCLKG1_MISC0CEN_ENABLED_U32);
}

//------------------------------------------------------------------------------
void gRTC0_enter_time_config(void)
{
   uint32_t i;
   // ENABLE CLOCK
   SI32_CLKCTRL_A_enable_apb_to_modules_1(SI32_CLKCTRL_0,
                                          SI32_CLKCTRL_A_APBCLKG1_MISC0CEN_ENABLED_U32);

   // SETUP MODULE (1)
   SI32_RTC_B_enable_bias_doubler (SI32_RTC_0);
   SI32_RTC_B_enable_autostep (SI32_RTC_0);
   SI32_RTC_B_enable_clock_out(SI32_RTC_0);
   SI32_RTC_B_enable_crystal_oscillator (SI32_RTC_0);
   // ENABLE MODULE
   SI32_RTC_B_enable_rtc_timer(SI32_RTC_0);

   // SETUP MODULE (2)
   for (i = 0; i < 70000; i++);   // Should wait 20 ms here before next poll
   while(!SI32_RTC_B_is_external_oscillator_valid(SI32_RTC_0));
   while(!SI32_RTC_B_is_load_capacitance_ready(SI32_RTC_0));
   return;
}

//-eof--------------------------------------------------------------------------
